<template>
  <view class="index">
    <view class="center">
      <view class="ind_from">
        <picker
          @change="bindPickerChange"
          :value="typeIndex"
          :range="typeArr"
          range-key="key"
          range-label="label"
        >
          <view class="uni-input">{{ typeArr[typeIndex].label }}</view>
        </picker>
        <input
          class="input"
          v-model="formData.keyword"
          placeholder="输入食物名称"
        />
        <view class="btn" @click="submit">{{
          isUnlock ? "计算" : "看广告获取计算结果"
        }}</view>
      </view>
      <view class="pub_ad_banner">
        <!-- #ifdef MP-TOUTIAO -->
        <ad
          :unit-id="adBannerIdDouyin"
          :ad-intervals="30"
          :fixed="false"
          type="banner"
          scale="100"
        />
        <!-- #endif -->
        <!-- #ifdef MP-KUAISHOU -->
        <ad :type="100011054" :unit-id="adBannerIdKuaishou"></ad>
        <!-- #endif -->
      </view>
    </view>
  </view>
</template>

<script>
import {
  showAdConfirmModal,
  adVideoCreate,
  adVideoShow,
  adBannerIdDouyin,
  adBannerIdKuaishou,
} from "@/common/ad";
export default {
  data() {
    return {
      adBannerIdDouyin: adBannerIdDouyin,
      adBannerIdKuaishou: adBannerIdKuaishou,
      showResult: false,
      isUnlock: false,
      typeIndex: 0,
      typeArr: [
        {
          key: 1,
          label: "谷类及制品",
        },
        {
          key: 2,
          label: "谷类及制2",
        },
      ],
      formData: {
        keyword: "",
        type: "",
      },
      result: {},
    };
  },
  onLoad() {
    adVideoCreate();
  },
  methods: {
    bindPickerChange(e) {
      // console.log("picker发送选择改变，携带值为", e.detail.value);
      this.typeIndex = e.detail.value;
    },
    selectGender(val) {
      this.formData.gender = val;
    },
    getResult() {
      this.calculate();
      this.isUnlock = true;
    },
    submit() {
      if (!this.isUnlock) {
        showAdConfirmModal(() => {
          try {
            adVideoShow(() => {
              this.getResult();
            });
          } catch (error) {
            this.getResult();
          }
        });
      } else {
        this.getResult();
      }
    },
  },
};
</script>

<style scoped lang="scss">
.banner {
  display: block;
  width: 100%;
}
.pub_radio {
  position: relative;
  padding-left: 50rpx;
}
.pub_radio:before {
  position: absolute;
  top: 50%;
  margin-top: -20rpx;
  left: 0;
  content: "";
  width: 40rpx;
  height: 40rpx;
  background-image: url("@/static/img/checked.png");
  background-size: cover;
}
.pub_radio.active:before {
  background-image: url("@/static/img/checked_active.png");
  background-size: cover;
}
.ind_from {
  background: #f9f5ef;
  margin-top: 20rpx;
  border: 4rpx solid #b39569;
  border-radius: 10rpx;
  .row {
    padding: 16rpx 20rpx;
    display: flex;
    border-bottom: 1px solid #ccc;
    &:last-child {
      border-bottom: 0;
    }
    .label {
      width: 170px;
      flex-shrink: 0;
    }
    .value {
      width: 100%;
      .input {
        width: 100%;
      }
    }
    .pub_radio {
      display: inline-block;
      margin-right: 40rpx;
    }
  }
}
.btn {
  margin-top: 30rpx;
  background: #db0000;
  color: #fff;
  text-align: center;
  padding: 14rpx 0;
  border-radius: 10rpx;
}
.sub_text {
  text-align: center;
  padding-top: 30rpx;
  font-size: 24rpx;
  color: #666;
  .red {
    color: red;
  }
  .bold {
    font-weight: bold;
  }
}
.pub_ad_banner {
  margin-top: 30rpx;
}
.refer {
  font-size: 14px;
  color: #8e8e8e;
  padding: 10px 10px;

  .panel {
    margin-bottom: 10px;
    .label {
      margin-bottom: 5px;
    }

    &:last-child {
      margin-bottom: 0;
    }
  }
}
.result {
  background: #f9f5ef;
  margin-top: 10px;
  padding: 10px 10px;

  .left,
  .right {
    width: 100%;
    padding: 10px 10px;
    background: #6f6fff;
    color: #fff;
  }

  .left {
    margin-right: 10px;
  }

  .p {
    display: block;

    .strong {
      font-size: 30px;
      display: inline-block;
      margin-right: 6px;
    }
  }
  .suffix {
    font-size: 22px;
    color: #000;
  }

  .title {
    text-align: center;
    font-size: 30px;
  }
  .desc {
    font-size: 32px;
    text-align: center;
    color: #ff1afe;
  }
}
</style>